package com.tourex.ui.view.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.tourex.C.bean.TourProjectInfo;
import com.tourex.ui.view.ExpandedGridView;

public class TourProjectInfoParentLayout extends ExpandedGridView {

	private String type;
	private int reqHeight;
	private List<TourProjectInfo> tpiList = new ArrayList<TourProjectInfo>();

	public TourProjectInfoParentLayout(Context context) {
		this(context, null);
	}

	public TourProjectInfoParentLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TourProjectInfoParentLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		setAdapter(adapter);
	}

	public void setData(List<TourProjectInfo> tpiList, String type, int reqHeight) {
		this.type = type;
		this.reqHeight = reqHeight;
		this.tpiList.clear();
		this.tpiList.addAll(tpiList);
		adapter.notifyDataSetChanged();
	}

	private BaseAdapter adapter = new BaseAdapter() {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TourProjectInfo tpi = (TourProjectInfo) getItem(position);
			View view;
			if (convertView == null) {
				view = new TourProjectInfoLayout(getContext());
			} else {
				view = convertView;
			}

			((TourProjectInfoLayout) view).setData(tpi, type, reqHeight);
			return view;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return tpiList.get(position);
		}

		@Override
		public int getCount() {
			return tpiList.size();
		}
	};

}
